package cate.game.activity.diamondbowl;

import cate.common.table.activity.diamondbowl.base.DiamondBowlBaseRow;
import cate.common.table.d.GD;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.crossrank.po.CrossRankActivity;
import cate.game.activity.diamondbowl.msg.DiamondBowlInfoResp;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass("聚宝盆-玩家数据")
public class DiamondBowlActivity extends RoleActivitySingle {
    @NoteField("储存的钻石数量")
    public long storedDiamondNum;
    @NoteField("今日购买次数")
    public int payTimesToday;
    @NoteField("上次跨天时间")
    public long lastDaySpanTime;
    @NoteField("支付的配置ID")
    public int payTid;

    @Override
    public void onRoleOpen() {
        lastDaySpanTime = TimeTool.getZeroTimeStamp(game().time.now());
        giftStore();
    }

    @Override
    public void onRoleEnd() {
        storedDiamondNum = 0;
        payTimesToday = 0;
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public void excelCheck() {
        // DO NOTHING
    }

    @Override
    public GameResult<Void> allowPay(PayItemRow pir) {
        GameResult<Void> r = new GameResult<>();
        if (storedDiamondNum <= 0) {
            return r.fail("当前没有储存钻石");
        }
        if (payTimesToday > 0) {
            return r.fail("今日购买次数已耗尽");
        }
        return r.success();
    }

    @Override
    public void onPaySuccess(PayItemRow pir) {
        if (storedDiamondNum == 0) {
            return;
        }
        DiamondBowlBaseRow baseRow = game().table.diamondBowl.base.get(pir.funcSubId);
        if (baseRow == null) {
            return;
        }
        long realAdd = (long) (storedDiamondNum * baseRow.multiple);
        if (realAdd <= 0) {
            return;
        }
        MixRes reward = new MixRes().addItem(new MixResItem(GDObj.Type.NUM, GDObj.Num.M1, realAdd));
        storedDiamondNum = 0;
        payTimesToday++;
        payTid = baseRow.id;
        new MixResAdder().setRes(reward)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .setOperation(GDOperation.ACT_DIAMOND_BOWL_REWARD)
                .setOperationSub(baseRow.id)
                .exe(role);
        noticeUpdate();
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        long nowTime = TimeTool.getZeroTimeStamp(game().time.now());
        if (payTimesToday == 0 || TimeTool.getDistanceDays(nowTime, lastDaySpanTime) > 1) {
            // 前一天没充值过或已经间隔2天以上 直接重置
            storedDiamondNum = 0;
        }
        payTimesToday = 0;
        payTid = 0;
        lastDaySpanTime = nowTime;
        giftStore();
        if (!silence) {
            noticeUpdate();
        }
    }

    /**
     * 同布消息到前端
     */
    private void noticeUpdate() {
        role.sendNow(new DiamondBowlInfoResp(this));
    }

    /**
     * 消费钻石记录
     *
     * @param consumeNum 消费数量
     */
    public void onConsumeDiamond(long consumeNum) {
        double scale = (double) DiamondBowlConstants.STORE_DEGREE / GD.W_10000;
        CrossRankActivity activity = role.getActivity().getEnableActivitySingle(GDFunc.Help.CROSS_DIAMOND_RANK);
        if (activity != null) {
            scale += 0.05d;
        }
        long add = (long) (consumeNum * scale);
        if (add > 0) {
            storedDiamondNum += add;
            noticeUpdate();
        }
    }

    /**
     * 赠送储存钻石
     */
    private void giftStore() {
        storedDiamondNum += game().table.diamondBowl.gift.getGiftNum();
    }
}
